package com.cws4cloud.cws.mall.file.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.mall.file.api.entity.Attachment;
import com.cws4cloud.cws.mall.file.biz.service.AttachmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 附件表
 *
 * @author zhulj
 * @date 2021-01-28 11:29:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/attachment" )
@Api(value = "attachment", tags = "附件表管理")
public class AttachmentController {

    private final  AttachmentService attachmentService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param attachment 附件表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('biz_attachment_get')" )
    public R getAttachmentPage(Page page, Attachment attachment) {
        return R.ok(attachmentService.page(page, Wrappers.query(attachment)));
    }


    /**
     * 通过id查询附件表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('biz_attachment_get')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(attachmentService.getById(id));
    }

    /**
     * 新增附件表
     * @param attachment 附件表
     * @return R
     */
    @ApiOperation(value = "新增附件表", notes = "新增附件表")
    @SysLog("新增附件表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('biz_attachment_add')" )
    public R save(@RequestBody Attachment attachment) {
        return R.ok(attachmentService.save(attachment));
    }

    /**
     * 修改附件表
     * @param attachment 附件表
     * @return R
     */
    @ApiOperation(value = "修改附件表", notes = "修改附件表")
    @SysLog("修改附件表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('biz_attachment_edit')" )
    public R updateById(@RequestBody Attachment attachment) {
        return R.ok(attachmentService.updateById(attachment));
    }

    /**
     * 通过id删除附件表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除附件表", notes = "通过id删除附件表")
    @SysLog("通过id删除附件表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('biz_attachment_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(attachmentService.removeById(id));
    }

	/**
	 * 附件上传
	 * @param file　文件
	 * @return Result
	 */
	@SysLog(value = "附件上传")
	@ApiOperation(value = "附件上传", notes = "附件上传")
	@PostMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file) {
		return attachmentService.upload(file);
	}
}
